/*
 * @Author: szx
 * @Date: 2022-05-15 13:50:35
 * @LastEditTime: 2022-05-15 14:17:35
 * @Description:
 * @FilePath: \leetcode\面试题\03\03\03.js
 */
/**
 * @param {number} cap
 */
var StackOfPlates = function (cap) {
    this.stack = [[]];
    this.cur = 0;
    this.size = cap;
};

/**
 * @param {number} val
 * @return {void}
 */
StackOfPlates.prototype.push = function (val) {
    if (!this.size) return null;
    if (this.stack[this.cur].length == this.size) {
        this.stack[++this.cur] = [];
    }
    this.stack[this.cur].push(val);
};

/**
 * @return {number}
 */
StackOfPlates.prototype.pop = function () {
    const res = this.stack[this.cur].pop();
    if (this.cur > 0 && this.stack[this.cur].length == 0) {
        this.stack.pop();
        --this.cur;
    }
    return res ?? -1;
};

/**
 * @param {number} index
 * @return {number}
 */
StackOfPlates.prototype.popAt = function (index) {
    if (index > this.cur) return -1;
    const res = this.stack[index].pop();
    if (this.cur > 0 && this.stack[index].length == 0) {
        this.stack.splice(index, 1);
        --this.cur;
    }
    return res ?? -1;
};
